/**
 *
 */
Ext.define('vacake.view.UsersAddressAdd', {
    extend: 'Ext.form.Panel',
    xtype: 'usersAddressAdd',
    requires: [
        'Ext.form.FieldSet'
    ],
    config: {
        style:'background:#EEEAE0',
        scrollable: {
            direction: 'vertical',
            directionLock: false
        },
        layout: 'vbox',
        items: [
            {
                docked: 'top',
                xtype: 'titlebar',
                title: '新增收货地址',
                items: [
                    {
                        text: '返回',
                        ui: 'action-back',
                        handler: function () {
                            var backid = Ext.Viewport.getInnerItems().length - 2;
                            Ext.Viewport.remove(this.up('usersAddressAdd'), true);
                            Ext.Viewport.setActiveItem(backid); 
                        }
                    }
                ]
            },
            {
                xtype: 'fieldset',
                cls:'x-container5',
                layout: 'vbox',
                defaults: {
                    labelWidth: '66px'
                },
                items: [
                    {
                        xtype: 'textfield',
                        name: 'name',
                        id: 'name',
                        label: '收货人:',
                        autoCapitalize: true
                    },
                    {
                        xtype: 'textfield',
                        name: 'mobile',
                        id: 'mobile',
                        label: '手机号码:',
                        autoCapitalize: true
                    },
                    {
                        xtype: 'textareafield',
                        name: 'address',
                        id: 'address',
                        label: '收货地址:',
                        autoCapitalize: true
                    },
                    {
                        xtype: 'hiddenfield',
                        name: 'cuid',
                        id: 'form_cuid'
                    },
                    {
                        xtype: 'hiddenfield',
                        name: 'cadid',
                        id: 'form_cadid'
                    },
                    {
                        xtype: 'button',
                        ui: 'confirm',
                        text: '新增',
                        id: 'assess',
                        listeners: {
                            tap: function () {
                                Ext.Viewport.mask({
                                    xtype: 'loadmask',
                                    message: '提交中...'
                                });
                                if (Ext.getCmp('name').getValue() == '') {
                                    Ext.Viewport.unmask();
                                    cordova.exec(function(message){}, function(error){}, "Messages", "showMsg", ['请填写收货人！']);
                                    return false;
                                }
                                if (Ext.getCmp('mobile').getValue() == '') {
                                    Ext.Viewport.unmask();
                                    cordova.exec(function(message){}, function(error){}, "Messages", "showMsg", ['请填写手机号码！']);
                                    return false;
                                }
                                if (!((/^13\d{9}$/g.test(Ext.getCmp('mobile').getValue())) || (/^15[0-35-9]\d{8}$/g.test(Ext.getCmp('mobile').getValue()))
                                    || (/^18[0-35-9]\d{8}$/g.test(Ext.getCmp('mobile').getValue())))) {
                                    Ext.Viewport.unmask();
                                    cordova.exec(function(message){}, function(error){}, "Messages", "showMsg", ['请填写正确的手机号码！']);
                                    return false;
                                }
                                if (Ext.getCmp('address').getValue() == '') {
                                    Ext.Viewport.unmask();
                                    cordova.exec(function(message){}, function(error){}, "Messages", "showMsg", ['请填写收货地址！']);
                                    return false;
                                }
                                this.up('usersAddressAdd').submit({
                                    url: Global.api_url + '/cloud/1/shipping_address_add',
                                    method: 'POST',
                                    scope: this,
                                    success: function (from, result) {
                                        Ext.Viewport.unmask();
                                        cordova.exec(function(message){}, function(error){}, "Messages", "showMsg", [result.Variables.Message.messagestr]);
                                        if (result.Variables.Result.code == 0) {
                                            var backid = Ext.Viewport.getInnerItems().length - 2;
                                            Ext.Viewport.remove(this.up('usersAddressAdd'), true);
                                            Ext.Viewport.setActiveItem(backid); 
                                        }
                                    },
                                    failure: function () {
                                        Ext.Viewport.unmask();
                                        //请求失败时执行操作
                                        cordova.exec(function(message){}, function(error){}, "Messages", "showMsg", ['请求失败,服务器维护中...']);
                                    }
                                });
                            }
                        }
                    }
                ]
            }
        ],
        listeners: {
            initialize: function () {
                Ext.getCmp('form_cuid').setValue(window.localStorage.getItem('app_Login_id'));
                Ext.getCmp('form_cadid').setValue(window.localStorage.getItem('app_district_id'));
             },
            painted: function () {
                Ext.Viewport.unmask();
            },back: function () {
                var backid = Ext.Viewport.getInnerItems().length - 2;
                Ext.Viewport.remove(Ext.Viewport.getActiveItem(), true);
                Ext.Viewport.setActiveItem(backid);
            }
        }
    }
});

